﻿using System;
using System.Collections.Generic;
using System.Linq;
using UcAsp.Net.Model;
namespace UcAsp.Net.NOSQLDAL
{
    public class UserInfoDAL : UcAsp.Net.IDAL.IUserInfoDAL
    {
        public List<UserInfo> GetUserInfo()
        {
            return STSData.GetInstance.Select<Model.UserInfo>();
        }
        public UserInfo Insert(UserInfo model)
        {
            long key;
            Model.UserInfo lastrow = STSData.GetInstance.LastRow<Model.UserInfo>(out key);
            model.ID = Convert.ToInt32(key.ToString()) + 1;
            STSData.GetInstance.Insert<Model.UserInfo>(model.ID, model);
            return model;
        }
        public bool Update(UserInfo model)
        {
            STSData.GetInstance.Update<Model.UserInfo>(model.ID, model);
            return true;
        }
        public bool Delete(UserInfo model)
        {
            STSData.GetInstance.Delete<Model.UserInfo>(model.ID);
            return true;
        }
        public UserInfo GetUserInfoByID(int id)
        {
            return GetUserInfo().FirstOrDefault(o => o.ID == id);
        }
        public List<UserInfo> SelectByKeyword(int pagesize, int pageindex, out int pagecount, string keyword)
        {
            List<UserInfo> list = new List<UserInfo>();
            var query = GetUserInfo().Where(o => o.UserName.Contains(keyword) || o.Email.Contains(keyword));
            pagecount = (query.Count() / pagesize) + 1;
            list = query.OrderBy(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
            return list;
        }
        public List<UserInfo> Select(int pagesize, int pageindex, out int pagecount)
        {
            List<UserInfo> list = new List<UserInfo>();
            var query = GetUserInfo();
            pagecount = (query.Count() / pagesize) + 1;
            list = query.OrderBy(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
            return list;
        }
        /// <summary>
        /// 用户排序
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagecount"></param>
        /// <param name="order">logintime,recommendid</param>
        /// <returns></returns>
        public List<UserInfo> Select(int pagesize, int pageindex, out int pagecount, string order)
        {
            List<UserInfo> list = new List<UserInfo>();
            var query = GetUserInfo();
            pagecount = (query.Count() / pagesize) + 1;
            switch (order.ToLower())
            {
                case "logintime":
                    list = query.OrderByDescending(o => o.LoginTime).Skip(pagesize * pageindex).Take(pagesize).ToList();
                    break;

                case "recommendid":
                    list = query.OrderByDescending(o => o.RecommendID).Skip(pagesize * pageindex).Take(pagesize).ToList();
                    break;

                default:
                    list = query.OrderByDescending(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
                    break;
            }
            return list;
        }
        public UserInfo GetUserInfoByNameAndPassword(string username, string password)
        {
            return GetUserInfo().FirstOrDefault(o => o.UserName == username && o.UserPassword == password);
        }
        public UserInfo GetUserInfoByNameAndOpenID(string username, string openid)
        {
            return GetUserInfo().FirstOrDefault(o => o.UserName == username && o.OpenID == openid);
        }
    }
}